package me.pplay.playtalk.view;

import java.util.Timer;
import java.util.TimerTask;

import me.pplay.playtalk.R;
import me.pplay.playtalk.util.AudioRecorder;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

public class AudioShowDialog{
	private Context context;
	protected PopupWindow mPopupWindow;
	private View mParentView;
	
	public AudioShowDialog(Context context, View mParentView){
		this.context 	 = context;
		this.mParentView = mParentView;
		init();
	}
	
	private void init(){
		View view 	= LayoutInflater.from(context).inflate(R.layout.layout_audio_showdialog, null);
		timeTv 		= (TextView) view.findViewById(R.id.timeTv);
		mPopupWindow = new PopupWindow(view, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    	mPopupWindow.setFocusable(true);
    	ColorDrawable dw = new ColorDrawable(0x00);
		mPopupWindow.setBackgroundDrawable(dw);
		
	}

	public void show(boolean bShow)
	{
		if (bShow)
		{		
			mPopupWindow.showAtLocation(mParentView, Gravity.TOP, 0, 0);
		}else{
			mPopupWindow.dismiss();
		}
	}

	private TextView timeTv;
	public String filePath;
	public int totalTime 			= 0;
	private final int SET_TIME_TV	= 1;
	private final int DELAYTIME 	= 1000;
	private AudioRecorder audioRecorder;
	private Timer timer;
	

	Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case SET_TIME_TV:
				setTimeTv(totalTime);
				break;
			}
		}
		
	};
	


	private void initTimer() {
		if(timer == null)
			timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {
			
			@Override
			public void run() {
				totalTime ++;
				handler.sendEmptyMessage(SET_TIME_TV);
			}
		}, DELAYTIME, DELAYTIME);
	}

	public void showDialog() {
		totalTime = 0;
		mPopupWindow.showAtLocation(mParentView, Gravity.TOP, 0, 0);
		audioRecorder 	= new AudioRecorder();
		filePath		= audioRecorder.startAudioRecorder();
		initTimer();
	}

	public void dismissDialog() {
		if(audioRecorder != null)
			audioRecorder.stopAudioRecorder();
		audioRecorder 	= null;
		filePath		= null;
		if(timer != null){
			timer.cancel();
			timer = null;
		}
		timeTv.setText("0:00");
//		handler = null;
//		System.gc();
//		this.dismiss();
		mPopupWindow.dismiss();
	}

	private void setTimeTv(int totalTime) {
		String timeStr 	= "";
		int m = totalTime % 60;
		if (m == 0) {
			timeStr = totalTime / 60 + ":00";
		} else {
			if (m < 10)
				timeStr = totalTime / 60 + ":0" + m;
			else
				timeStr = totalTime / 60 + ":" + m;
		}
		if(timeTv != null)
			timeTv.setText(timeStr);
	}
	
}
